<template>
  <div class="app-container">
    <el-table :data="blogs" style="width: 100%" border>
      <el-table-column label="序号" width="60" align="center">
        <template slot-scope="scope">{{
          scope.$index + (curPage - 1) * limit + 1
        }}</template>
      </el-table-column>
      <el-table-column label="文章名称" align="center">
        <template slot-scope="scope">
          <el-popover placement="top-start" title="博客预览图" trigger="hover" width="230">
            <el-image
              style="width: 200px;"
              :src="scope.row.thumb"
              fit="contain"
              :preview-src-list="srcList"
            ></el-image>
            <a
              href="#"
              target="_blank"
              @click.prevent="toBlog(scope.row)"
              slot="reference"
              >{{ scope.row.title }}</a
            >
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="文章描述" align="center">
        <template slot-scope="scope">{{ scope.row.description }}</template>
      </el-table-column>
      <el-table-column label="浏览数" width="80" align="center">
        <template slot-scope="scope">{{ scope.row.scanNumber }}</template>
      </el-table-column>
      <el-table-column label="评论量" width="80" align="center">
        <template slot-scope="scope">{{ scope.row.commentNumber }}</template>
      </el-table-column>
      <el-table-column
        label="所属分类"
        width="100"
        align="center"
      >
        <template slot-scope="scope">{{ scope.row.category ? scope.row.category.name : "未分类" }}</template>
      </el-table-column>
      <el-table-column label="创建日期" align="center">
        <template slot-scope="scope">{{ scope.row.createDate }}</template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            content="编辑"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              @click="handleEditBlog(scope.row)"
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="dark"
            content="删除"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="deleteBlog(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="margin-top: 30px"
      background
      layout="prev, pager, next, total, ->, sizes, jumper"
      :current-page="curPage"
      :total="count"
      :page-size="limit"
      :page-sizes="[5, 10, 20]"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      @prev-click="handleClickPrev"
      @next-click="handleClickNext"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getBlog, deleteBlog } from "@/api/blog.js";
import { formatDate } from "@/utils/formatDate.js";
import { SERVER_URL, CLIENT_URL } from "@/urlConfig.js";

export default {
  data() {
    return {
      curPage: 1,
      limit: 10,
      totalPage: 1,
      count: 0,
      blogs: [],
      srcList: []
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      var resp = await getBlog(this.curPage, this.limit);
      this.blogs = resp.data.rows.map((item) => {
        var newItem = {
          ...item,
          createDate: formatDate(item.createDate),
          thumb: SERVER_URL + item.thumb,
        };
        this.srcList.push(newItem.thumb);
        return newItem;
      });
      this.count = resp.data.total;
      this.totalPage = Math.ceil(this.count / this.limit);
      if (this.curPage > this.totalPage && this.totalPage !== 0) {
        this.curPage = this.totalPage;
        this.fetchData();
      }
    },
    toBlog(blogInfo) {
      window.open(`${CLIENT_URL}/blog/${blogInfo.id}`);
    },
    async deleteBlog(blogInfo) {
      this.$confirm("此操作将永久删除该文章, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await deleteBlog(blogInfo.id);
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.fetchData();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSizeChange(size) {
      this.limit = size;
      this.curPage = 1;
      this.fetchData();
    },
    handleCurrentChange(pageNum) {
      this.curPage = pageNum;
      this.fetchData();
    },
    handleClickPrev() {
      this.currentPage -= 1;
    },
    handleClickNext() {
      this.currentPage += 1;
    },
    handleEditBlog(blogInfo){
      this.$router.push(`/blog/editblog/${blogInfo.id}`);
    }
  },
};
</script>

<style>
</style>
